package com.tilaiedu.mvc.controller;

import com.tilaiedu.mvc.entity.User;
import com.tilaiedu.mvc.service.UserService;
import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.GifCaptcha;
import com.wf.captcha.utils.CaptchaUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Arrays;

/**
 * @Author xlzhang
 * @Date 2022/5/25 11:07
 * @Version 1.0
 * @Description TODO
 */
@Slf4j
@Controller
public class UserController {
     //Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserService userService;

    /**
     * 退出
     * @param session
     * @return
     */
    @GetMapping("/logout")
    public String logout(HttpSession session){
        // 让当前的session直接失效，
        session.invalidate();
        // 跳转到登录页
        return "redirect:/login.html";
    }

    // 验证码
    @RequestMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 去除jdk11的警告
        System.setProperty("nashorn.args","--no-deprecation-warning");
        // 算数
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(120, 38);
        // captcha.setLen(3);  // 几位数运算，默认是两位
        // 指定验证码对象
        CaptchaUtil.out(captcha,request, response);
    }

    @PostMapping("/login")
    public String login(User user,
                        String checkCode,
                        @RequestParam(defaultValue = "false") boolean saveme,
                        HttpServletRequest request,
                        HttpServletResponse response,
                        HttpSession session){

        log.debug("user = " + user);
        log.debug("checkCode = " + checkCode);
        log.debug("---saveme = {}",saveme);
        // 判断验证码是否正确
        if (!CaptchaUtil.ver(checkCode, request)) {
            CaptchaUtil.clear(request);  // 清除session中的验证码
            log.debug("----验证码错误");
            session.setAttribute("info","验证码错误");
            // 验证码错误。直接返回到登录页
            return "redirect:/login.html";
        }
        CaptchaUtil.clear(request);  // 清除session中的验证码
        // 查询数据库中是否有正确的用户信息
        User queryUser = userService.login(user.getAccount(), user.getPassword());
        log.debug("queryUser = " + queryUser);
        if (queryUser == null){
            log.debug("账户或密码错误");
            session.setAttribute("info","账户或密码错误");
            return "redirect:/login.html";
        }
        // 实现记住我功能(7天免登陆)
        // 将数据库返回的有完整信息的用户对象保存到session中
        session.setAttribute("user",queryUser);
        // 获取当前session id值
        String sessionId = session.getId();
        // 10CDEAB2E44523A914E7CD430749E3F2
        log.debug("jsessionid = {}",sessionId);
        // 创建一个cookie
        Cookie cookie = new Cookie("JSESSIONID", sessionId);
        if (saveme) {
            // 设置cookie的存活时长
            cookie.setMaxAge(60*60*24*7);
        } else {
            // 负数代表回话默认状态(回话结束就删除)
            cookie.setMaxAge(-1);
        }
        // 覆盖默认的，只需要添加到response中
        response.addCookie(cookie);
        // 登录成功,重定向到数据页面
        return "redirect:/index.html";
    }

}











